package zz.demo.springCloud.config.securityService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import javax.sql.DataSource;


/**
 * Created by zhangzheng on 2017-8-11.
 */
@Configuration
public class CustomOauth2Configuration {

    @Configuration
    @EnableResourceServer
    protected static class CustomResourceConfiguration extends ResourceServerConfigurerAdapter {

        @Override
        public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
            resources.resourceId("user-id");
        }

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http.antMatcher("/me").authorizeRequests().anyRequest().authenticated()
            ;
        }
    }

    @Configuration
    @EnableAuthorizationServer
    protected static class CustomAuthorizationConfiguration extends AuthorizationServerConfigurerAdapter{

//        @Autowired
//        AuthenticationManager authenticationManager;

//        @Autowired
//        RedisConnectionFactory redisConnectionFactory;

        @Autowired
        DataSource dataSource;

//        @Bean
//        public ClientDetailsService custom(){
//            return new CustomClientDetailService();
//        }

//        @Bean
//        public TokenStore tokenStore(){
//            JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
//            converter.setSigningKey("sign");
//            return new JwtTokenStore(converter);
//        }


        @Override
        public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
            security.allowFormAuthenticationForClients();
        }

        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
//            clients.withClientDetails(new JdbcClientDetailsService(dataSource) );
            clients.inMemory()
                    .withClient("user-id")
                    .secret("123456")
                    .authorizedGrantTypes("authorization_code")
                    .scopes("read")
                    ;
        }


        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
            endpoints
                    .tokenStore(new InMemoryTokenStore())
//            .tokenStore(this.tokenStore())
//                    .authenticationManager(authenticationManager)
            ;
        }
    }
}
